<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Request;
use think\Db;

class Address extends Api
{
    public function updete()
    {
    	//地址修改,新增
        if($this->request->isPost())
        {
            $post = input('post.');
                Db::startTrans();
            try{
                $post = array_filter($post);
                if(isset($post['id'])){
                    if(isset($post['status'])){
                        if($post['status'] == 2)
                        {
                            $userall = model($this->request->controller())->field('id,status')->where([
                                ['user_id','=',$post['user_id']],
                                ['status','=',2]
                            ])->select();
                            if($userall){
                                $userall = $userall->toArray();
                                foreach ($userall as $k=>$v){
                                    $userall[$k]['status'] = 1;
                                }
                                model($this->request->controller())->saveAll($userall);
                            }
                        }
                    }
                    $id = $post['id'];
                    $post['id'] = '';
                    $post = array_filter($post);
                    $model = model('order')->save($post,['id'=>$id]);
                }else{
                    if(!isset($post['content']) || !isset($post['phone']) || !isset($post['name']) || !isset($post['user_id']))
                    {
                        return fh(404,'error','参数不完整',[]);
                    }
                    $address = model('model')->find();
                    if($address){
                    	$model = model($this->request->controller())->save($post);
                    }else{
                    	$post['status'] = 2;
                    	$model = model($this->request->controller())->save($post);
                    }
                }
                Db::commit();
            }catch (\Exception $e){
                Db::rollback();
                return fh(500,'error',$e->getMessage(),[]);
            }
            return fh(200,'success','请求成功',$model);
        }
    }
    //数据详情
    public function view()
    {
        if($this->request->isPost()){
            $post = input('post.');
            $post = array_filter($post);
            if($post){
                if(!isset($post['id']))
                {
                    return fh(404,'error','参数不完整',[]);
                }
                $list = model($this->request->controller())->where('id','=',$post['id'])->find()->toArray();
                return fh(200,'success','请求成功',$list);
            }else{
                return fh(404,'error','没有post数据',[]);
            }
        }
    }
    //修改地址默认选中
    public function status()
    {
        if($this->request->isPost())
        {
            $post = input('post.');
            $post = array_filter($post);
            if($post){
                if(!isset($post['status']) || !isset($post['id']) || !isset($post['user_id']))
                {
                    return fh(404,'error','参数不完整',[]);
                }
                $userall = model($this->request->controller())->field('id,status')->where('user_id','=',$post['user_id'])->select()->toArray();
                foreach ($userall as $k=>$v){
                    if($userall[$k]['status'] == 2){
                        $userall[$k]['status'] = 1;
                    }
                }
                model($this->request->controller())->saveAll($userall);
                if($post['status'] == 2){
                    model($this->request->controller())->save(['status'=>$post['status']],['id'=>$post['id']]);
                }
                return fh(200,'success','修改成功',true);
            }else{
                return fh(404,'error','没有post数据',[]);
            }
        }else{
            return fh(404,'error','不是post请求',[]);
        }
    }
}
